﻿using AutoMapper;
using B.S.RbacData.Domain;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.RBAC;
using B.S.RbacData.Read.API.Dtos;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.RBAC
{
    /// <summary>
    /// 获取权限命令处理
    /// </summary>
    public class GetAllPermissionCommandhandlter : IRequestHandler<GetAllPermissionCommand, ApiPaging<GetAllPermissionDtos>>
    {
        private readonly IBaseRepository<PermissionModel> permissionRep;
        private readonly IMapper mapper;

        public GetAllPermissionCommandhandlter(IBaseRepository<PermissionModel> PermissionRep,IMapper mapper)
        {
            permissionRep = PermissionRep;
            this.mapper = mapper;
        }


        public async Task<ApiPaging<GetAllPermissionDtos>> Handle(GetAllPermissionCommand request, CancellationToken cancellationToken)
        {

            //获取权限信息
            var permission = permissionRep.GetAll().AsNoTracking().Where(x=>x.IsDeleted==false).ToList();


            return new ApiPaging<GetAllPermissionDtos>()
            {

                code = ResultEnumCode.Ok,
                message = "获取成功",
                PageData = mapper.Map<List<GetAllPermissionDtos>>(permission),

            };
        }
    }
}
